import Doctormanagement from './Hospital Information'
import Booking from './Booking Management/index.js'
// 导入费用管理二级路由
import costManagement from './liuhuan'
import { Navigate } from 'react-router-dom'
import { lazy, Suspense } from 'react'

var Index = lazy(() => import('../views/Index'))
var Login = lazy(() => import('../views/Login'))
var Register = lazy(() => import('../views/Register'))
var NotFound = lazy(() => import('../views/NotFound'))

var Home = lazy(() => import('../views/Index/Home'))
var ExpenseManagement = lazy(() => import('../views/Index/ExpenseManagement'))
// var HospitalInformation = lazy(() => import('../views/Index/HospitalInformation'))
var Systemsettings = lazy(() => import('../views/Index/Systemsettings'))

var Shouye = lazy(() => import('../views/Index/Home/shouye'))



var Systemsettingsadministrator = lazy(() => import('../views/Index/System settings/Systemsettingsadministrator'))
var Role = lazy(() => import('../views/Index/System settings/Role'))
var Professionaltitle = lazy(() => import('../views/Index/System settings/Professionaltitle'))
var News = lazy(() => import('../views/Index/System settings/News'))
var Changepassword = lazy(() => import('../views/Index/System settings/Changepassword'))
var Addadmin = lazy(() => import('../views/Index/System settings/Addadmin'))
var Addnews = lazy(() => import('../views/Index/System settings/Addnews'))
var Addrole = lazy(() => import('../views/Index/System settings/AddRole'))

function AuthComponent({ children, permission }) { //children 是需要渲染的组件

    //获取当前用户的权限列表
    var permissions = localStorage.getItem('userinfo') ?
        JSON.parse(localStorage.getItem('userinfo')).role.permission : [];

    //判断当前用户是否有权限访问该路由对应的组件
    var res = permission && permissions.includes(permission)

    return (
        <Suspense fallback={<div>loading...</div>}>
            {
                //如果res为true,则表示有权限访问,渲染children对应的组件
                //如果res为false,则表示没有权限访问,渲染NotFound组件
                res ?
                    children :
                    <NotFound />
            }
        </Suspense>
    )
}
var routes = [
    {
        path: '/index',
        element: <Index />,
        children: [
            {
                path: '/index/home',
                element: <AuthComponent permission={11}><Home /></AuthComponent>,
                children: [
                    {
                        path: '/index/home/shouye',
                        element: <AuthComponent permission={111}><Shouye /></AuthComponent>,
                    }
                ]
            },
            // 预约管理
            ...Booking,
            
            // 费用管理
            ...costManagement,

            // 医院信息
            ...Doctormanagement,

            // 系统设置
            {
                path: '/index/systemsettings',
                element: <AuthComponent permission={15}><Systemsettings /></AuthComponent>,
                children: [
                    {
                        path: '/index/systemsettings/administrator',
                        element: <AuthComponent permission={151}><Systemsettingsadministrator /></AuthComponent>,
                    },
                    {
                        path: '/index/systemsettings/role',
                        element: <AuthComponent permission={152}><Role /></AuthComponent>,
                    },
                    {
                        path: '/index/systemsettings/professionaltitle',
                        element: <AuthComponent permission={153}><Professionaltitle /></AuthComponent>,
                    },
                    {
                        path: '/index/systemsettings/news',
                        element: <AuthComponent permission={154}><News /></AuthComponent>,
                    },
                    {
                        path: '/index/systemsettings/changepassword',
                        element: <AuthComponent permission={155}><Changepassword /></AuthComponent>,
                    },
                    {
                        path: '/index/systemsettings/addadmin',
                        element: <Addadmin />,
                    },
                    {
                        path: '/index/systemsettings/addrole',
                        element: <Addnews />,
                    },
                    {
                        path: '/index/systemsettings/addnews',
                        element: <Addrole />,
                    }

                ]
            },

            // 二级路由重定向
            {
                path: '/index',
                element: <Navigate to='/index/home' />
            },
            {
                path: '*',
                element: <NotFound />
            }

        ]
    },
    {
        path: '/login',
        element: <Login />
    },
    {
        path: '/register',
        element: <Register />
    },
    {
        path: '/',
        element: <Navigate to='/login' />
    },
    {
        path: '/notFound',
        element: <NotFound />
    }
]
export default routes;